package com.promise.jokerdream.work.signup

import com.promise.jokerdream.work.BaseWork
import com.promise.jokerdream.work.WorkResult
import com.promise.jokerdream.repository.GameApi
import com.promise.jokerdream.repository.PhoneGameApi
import com.promise.jokerdream.repository.foldWithApiException
import com.promise.jokerdream.database.entity.WulinGameConfigEntity
import com.promise.jokerdream.config.game.WulinRegistrationType
import com.promise.jokerdream.manager.ExecutionHistoryManager
import com.promise.jokerdream.utils.AppContext
import java.util.Calendar

// 武林大会工作
class WulinWork(
    private val config: WulinGameConfigEntity,
    override val uin: String
): BaseWork(uin) {
    
    private val gameApi = GameApi.Companion.getInstance()
    private val phoneGameApi = PhoneGameApi.Companion.getInstance()
    private val historyManager by lazy { ExecutionHistoryManager.Companion.getInstance(AppContext.get()) }
    
    override val name: String = "武林大会"
    
    override suspend fun executeWork(): WorkResult {
        try {
            // 检查报名时间：13:00-24:00
            val currentHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY)
            if (currentHour < 13) {
                return WorkResult.Success
            }
            
            // 1. 查询武林大会状态
            val queryResult = gameApi.queryWulin()
            
            queryResult.foldWithApiException(
                onSuccess = { response ->
                    // 检查是否需要报名
                    if (response.join == 0) {
                        val registrationType = try { WulinRegistrationType.valueOf(config.registrationType) } catch (e: Exception) { WulinRegistrationType.COMPUTER }
                        when (registrationType) {
                            WulinRegistrationType.MOBILE -> {
                                // 手机报名
                                val signupResult = phoneGameApi.signWulin()
                                signupResult.fold(
                                    onSuccess = { signupResponse ->
                                        historyManager.addHistory(name, signupResponse.actionResult)
                                    },
                                    onFailure = { exception ->
                                        historyManager.addHistory(name, exception.message)
                                    }
                                )
                            }
                            WulinRegistrationType.COMPUTER -> {
                                // 电脑报名
                                val winner = response.info.firstOrNull()
                                val signupResult = gameApi.signupWulin(winner?.id.orEmpty())
                                signupResult.foldWithApiException(
                                    onSuccess = { signupResponse ->
                                        historyManager.addHistory(name, signupResponse.getDescription())
                                    },
                                    onFailure = { code, msg ->
                                        historyManager.addHistory(name, msg, code)
                                    }
                                )
                            }
                        }
                    }
                },
                onFailure = { code, msg ->
                    historyManager.addHistory(name, msg, code)
                    return WorkResult.Failure(Exception(msg), msg)
                }
            )
            
            return WorkResult.Success
        } catch (e: Exception) {
            historyManager.addHistory(name, e.message)
            return WorkResult.Failure(e, e.message)
        }
    }
}